﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: HDD
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


var mod_sysmonitor_plugin_hdd_physicaldevice = new Array();
var mod_sysmonitor_plugin_hdd_refstring = "a";

// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["phpp_hdd"] = new Object();
myPlugins["phpp_hdd"]["name"] = getLocalizedString2("mod_sysm_plugin_hdd_name");
myPlugins["phpp_hdd"]["onlyonce"] = false;
myPlugins["phpp_hdd"]["newObject"] = function (NewSettings) { return new plugin_hdd_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_hdd_sensor(NewSettings) {
	// Required variables
	this.plugin = "phpp_hdd";
	this.settings = NewSettings;
	this.edit = plugin_hdd_edit;
	this.ReDraw = plugin_hdd_ReDraw;
	//this.Delete = plugin_phpp_mem_Delete;
	this.Update = plugin_hdd_update;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	// Init Const
	this.mapping = new Object();
	for (var a in filesystem.volumes ) {
		this.mapping[filesystem.volumes[a].path] = a;
	}


	// Init Settings
	if(!this.settings.drive) this.settings.drive = 0;
	if(!this.settings.label) this.settings.label = "";
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#0000FF";
	if(!this.settings.alert) this.settings.alert = 5;
	if(!this.settings.baralertcolor) this.settings.baralertcolor = "#FF0000";
	if(!this.settings.text) this.settings.text = 0;
	if(!this.settings.id)
		for(var i in PHPSysInfo)
			if(i) {
				this.settings.id = i;
				//break;
			}



	// The Sensor
	this.frame.onMultiClick = function ( ) { plugin_hdd_opendevice.call(obj); };
	this.drive = new class_PercentBar(this.frame,0,0,"");
	this.drive.PercentColor(this.settings.color);
	this.drive.PercentBarColor(this.settings.barcolor);
	if(this.settings.label != "") this.drive.text_name.data = this.settings.label;
	if(this.drive.text_name.data == "") this.drive.text_name.data = "("+this.settings.drive+")";


	// Update Timer
	var obj = this;
	plugin_hdd_update.call(obj);


	// Calculating sensore height
	this.frame.height = this.drive.background.vOffset + this.drive.background.height;
	return true;


// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_hdd_edit() {
		var availableDrives = new Array();
		var availableDrivesValue = new Array();
		var tmpDevice, tmpSize;
		for (var a in PHPSysInfo[this.settings.id]["FileSystem"] ) {
			tmpDevice =  PHPSysInfo[this.settings.id]["FileSystem"][a]["Device"];
			while(tmpDevice.length < 15) tmpDevice += " ";
			tmpSize = "(" + bytesToUIString( 1024 * PHPSysInfo[this.settings.id]["FileSystem"][a]["Size"] ) + ")"
			availableDrives[a] = tmpDevice + tmpSize;
			availableDrivesValue[a] = a;
		}

		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_drive");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = availableDrives;
		Drive_Edit_DialogFields[FieldID].optionValue = availableDrivesValue;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.drive;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_label");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.label;
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_hdd_pref_labeldesc");

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_text");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_hdd_pref_free"),getLocalizedString2("mod_sysm_plugin_hdd_pref_used"),getLocalizedString2("mod_sysm_plugin_hdd_pref_total"));
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1,2);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.text;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_alert");
		Drive_Edit_DialogFields[FieldID].type = 'slider';
		Drive_Edit_DialogFields[FieldID].minLength = 0;
		Drive_Edit_DialogFields[FieldID].maxLength = 100;
		Drive_Edit_DialogFields[FieldID].ticks = 21;
		Drive_Edit_DialogFields[FieldID].tickLabel = new Array("0%","25%","50%","75%","100%");
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_hdd_pref_alertdesc");
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.alert;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_baralertcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.baralertcolor;

		var SList = new Array();
		var SVal = new Array();
		for(var i in PHPSysInfo) {
			SList[SList.length] = PHPSysInfo[i]["Vitals"]["Hostname"];
			SVal[SVal.length] = i;
		}

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_php_host_source");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = SList;
		Drive_Edit_DialogFields[FieldID].optionValue = SVal;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.id;




		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.drive.PercentColor(this.settings.color);
			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.drive.PercentBarColor(this.settings.barcolor);
			this.settings.label = Drive_Edit_DialogResults[3];
			this.settings.drive = Drive_Edit_DialogResults[2];

			if(this.settings.label == "") this.drive.text_name.data = basename(PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Device"]);
			else this.drive.text_name.data = this.settings.label;

			this.settings.text = Drive_Edit_DialogResults[4];
			this.settings.alert = Drive_Edit_DialogResults[5];
			this.settings.baralertcolor = Drive_Edit_DialogResults[6];

			this.settings.id = Drive_Edit_DialogResults[7];

			plugin_hdd_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_hdd_update() {
		if(PHPSysInfo[this.settings.id]) {
			if(this.settings.text==1)
				this.drive.PercentBarValue( 100 * PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Used"] / PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Size"] ,bytesToUIString( 1024 * PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Used"] ));
			else if(this.settings.text==2)
				this.drive.PercentBarValue( 100 * PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Used"] / PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Size"] ,bytesToUIString( 1024 * PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Sie"] ));
			else
				this.drive.PercentBarValue( 100 * PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Used"] / PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Size"] ,bytesToUIString( 1024 * (PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Size"] - PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Used"]) ));

			if(this.settings.label == "") this.drive.text_name.data = basename(PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Device"]);
			else this.drive.text_name.data = this.settings.label;

			if(this.settings.alert>0) {
				if( (100 - ( 100 * PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Used"] / PHPSysInfo[this.settings.id]["FileSystem"][this.settings.drive]["Size"]) ) < this.settings.alert ) {
					this.drive.PercentBarColor(this.settings.baralertcolor);
				} else {
					this.drive.PercentBarColor(this.settings.barcolor);
				}
			}

		} else {
			this.drive.PercentBarValue( 0 ,"---");
		}
		return true;
	}


	function plugin_hdd_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.drive.style = preferences.mod_sysmonitor_bar_style.value;
		this.drive.size = mod_sysmonitor_size;
		this.frame.height = this.drive.background.vOffset + this.drive.background.height;
		return true;
	}

function basename(path) {
    return path.replace( /.*\//, "" );
}

}

// --------------------------------------------------------------------------------
// The END